﻿using UnityEngine;

namespace Assets.Scripts.Interface
{
    abstract class InterfaceBase : MonoBehaviour
    {
        internal Texture FadeTexture { get; set; }
        protected bool Ready { get; private set; }
        protected bool Finishing { get; set; }
        protected bool Finished { get; private set; }
        private float alpha;

        internal void OnEnable()
        {
            alpha = 1;
            Ready = Finishing = Finished = false;
        }

        internal void Update()
        {
            if (Finished)
                Execute();
        }

        abstract protected void Execute();

        void OnGUI()
        {
            if (Finished)
                return;
            if (!Ready)
                Ready = FadeIn();
            else if (Finishing)
                Finished = FadeOut();
        }

        protected bool ButtonClick()
        {
            if (Ready && !Finishing)
            {
                Finishing = true;
                return true;
            }
            return false;
        }

        private bool FadeIn()
        {
            alpha = Mathf.Clamp01(alpha - 0.05f);
            GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, alpha);
            GUI.depth = -1000;
            GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), FadeTexture);
            return alpha <= 0;
        }

        private bool FadeOut()
        {
            alpha = Mathf.Clamp01(alpha + 0.05f);
            GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, alpha);
            GUI.depth = -1000;
            GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), FadeTexture);
            return alpha >= 1;
        }
    }
}
